<?php 
class productsAction extends AdminMaster{
	public function listFilter(){
		$this->data['filter']['cates']		= ProductCates::getDataListFrontEnd();
		$this->data['filter']['manufacturers']		= ProductManufacturers::getDataListFrontEnd();
		$this->data['filter']['os']				= OsEnvironments::getDataListFrontEnd();
		$this->data['filter']['classes']	= ProductClasses::getDataListFrontEnd();
		$this->data['filter']['levels']		= ProductLevels::getDataListFrontEnd();
		
		$this->data['form']['albums'] 		= GalleryAlbums::getDataListFrontEnd();
	}
	public function showList(){
		$this->data['list']			 			= Products::getDataList($this->data['filter'], $this->data['pagination']);
		$this->data['pagination']['total']		= Products::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'products_product';
		$this->data['pagination']['params']		= $this->data['filter'];
	}
	public function delRecord(){
		ProductsTable::delete($this->data['select_list']);
	}
	public function disableRecord(){
		ProductsTable::disable($this->data['select_list']);
	}
	public function enableRecord(){
		ProductsTable::enable($this->data['select_list']);
	}
	public function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		Products::saveSortOrder($sortData);
	}
	public  function setAttribute(){
		if(ProductsTable::setHot($this->data['id'], $this->getRequest()->getParameter('val', 0))){
			echo json_encode(array('id' => $this->data['id'], 'val' => $this->getRequest()->getParameter('val', 0)));
		}
		die;
	}
	public function addnew(){
		$this->setTemplate('productsForm');

		$form = new ProductsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			
			if($form->isValid()){
				$product_id = Products::insertUpdate($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success');
					$form = new ProductsFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error'), sfConfig::get('app_email_admin')) ;
				}
			}
		}
		$this->setVar('form', $form);
	}
	public function edit(){
		$this->setTemplate('productsForm');

		$form = new ProductsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(Products::insertUpdate($this->formData)){
					$this->data['result']	= __('edit_success');
				}else{
					$this->data['result']	= sprintf(__('edit_error'), sfConfig::get('app_email_admin')) ;
				}
			}
			if(!empty($this->formData['photo_url'])){
				$this->data['photo_url'] = $this->formData['photo_url'];
				$this->data['photo_remove']	= 1;
			}
		}else{
			$data = Products::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				if(!empty($data['photo_url'])){
					$this->data['photo_url'] = $data['photo_url'];
					$this->data['photo_remove'] = 1;
				}
				
				$this->formData = array(
						'cate_id'						=> $data['cate_id'],
						'product_manufacturer_id'		=> $data['product_manufacturer_id'],
						'os_environment_id'				=> $data['os_environment_id'],
						'class_id'						=> $data['class_id'],
						'level_id'						=> $data['level_id'],
						
						'photo_url'					=> $data['photo_url'],
						'video_url'					=> $data['video_url'],
						'price'							=> $data['price'],
						'quantity'					=> intval($data['quantity']),
						'code'						=> $data['code'],
						'is_enabled'				=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				foreach($this->data['langs'] as $lang){
					$this->formData['sub_name_'.$lang] = $data['ProductsI18n'][$lang]['sub_name'];
					$this->formData['name_'.$lang] = $data['ProductsI18n'][$lang]['name'];
					$this->formData['description_'.$lang] = $data['ProductsI18n'][$lang]['description'];
					$this->formData['content_'.$lang] = $data['ProductsI18n'][$lang]['content'];
						
				}
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);
	}
}